package com.snmp.smn.monitors;

import java.sql.DriverManager;

import com.snmp.smn.beans.Device;
import com.snmp.smn.utils.EmailSender;
import com.snmp.smn.utils.Utilities;

public class MySQLDBMonitor extends DeviceMonitor {
	
	public MySQLDBMonitor(Device device) {
		this.device = device;
	}

	public void checkDevice() throws Exception {
		Utilities.log("Checking device " + device);
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://" + device.getDeviceIP() + ":" + device.getDevicePort();
			DriverManager.getConnection(url, "root", "root");
			
			Utilities.log("Successfully checked device " + device);
			
			updateSuccessfulCheck();
		} catch (Exception e) {
			Utilities.log("Unabled to check device " + device);
			
			updateFailedCheck();
			
			EmailSender.sendDeviceFailed(device.getDeviceId(), device.getDeviceName(), 
											device.getDeviceIP(), device.getDevicePort(),
												e.getMessage());
			
			restartDevice();
		}
	}
}
